package com.star.goods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.star.goods.dao.BrandMapper;
import com.star.goods.pojo.Brand;
import com.star.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \_________
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \        \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \        \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \        \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\        \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|         \
	 	  \___________________________________________________________________________________\

*/
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    /**
     * 品牌列表
     * @return
     */
    @Override
    public List<Brand> findList() {
        return brandMapper.selectAll();
    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增品牌
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    /**
     * 修改品牌信息
     * @param brand
     */
    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 删除品牌
     * @param id
     */
    @Override
    public void delById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 品牌条件查询
     * @param searchMap
     * @return
     */
    @Override
    public List<Brand> list(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        //封装查询条件
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //品牌查询(模糊查询)
            if (searchMap.get("name") != null && !searchMap.get("name").equals("")) {
                criteria.andLike("name", "%"+searchMap.get("name")+"%");
            }
            //按照首字母查询(精确查询)
            if (searchMap.get("letter") != null && !searchMap.get("letter").equals("")) {
                criteria.andEqualTo("letter", searchMap.get("letter"));
            }
        }
        List<Brand> brandList = brandMapper.selectByExample(example);
        return brandList;
    }

    /**
     * 分页查询
     * @param page 当前页码
     * @param size 每页信息
     * @return
     */
    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Brand> brands = (Page<Brand>) brandMapper.selectAll();
        return brands;
    }

    /**
     * 条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        //分页
        PageHelper.startPage(page, size);

        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        //品牌查询(模糊查询)
        if (searchMap.get("name") != null && !searchMap.get("name").equals("")) {
            criteria.andLike("name", "%"+searchMap.get("name")+"%");
        }
        //按照首字母查询(精确查询)
        if (searchMap.get("letter") != null && !searchMap.get("letter").equals("")) {
            criteria.andEqualTo("letter", searchMap.get("letter"));
        }
//        criteria.andAllEqualTo(searchMap);
        Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);

        return pageInfo;
    }

    /**
     * 根据商品分类查询品牌列表
     * @param categoryName
     * @return
     */
    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        List<Map> brandList = brandMapper.findListByCategoryName(categoryName);
        return brandList;
    }
}
